package electronet.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.util.Conversions;

import electronet.excepcion.DAOExcepcion;
import electronet.negocio.GestionPersona;

/**
 * Servlet implementation class InsertarPersona
 */
@WebServlet("/InsertarPersona")
public class InsertarPersona extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public InsertarPersona() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String sTipoPersona = request.getParameter("TipodePersona");
		String sNombre = request.getParameter("name");
		String sPaterno = request.getParameter("apellidos_paterno");
		String sMaterno = request.getParameter("apellidos_materno");
		String sSexo = request.getParameter("Sexo");
		String sTipoDoc = request.getParameter("TipoDocumento");
		String sNroDoc = request.getParameter("Nrodocumento");
		String sMail = request.getParameter("email");
		String sCelular = request.getParameter("celular");
		String sFechaNac = request.getParameter("fechanac");
		
		String sNickname = request.getParameter("Nickname");
		String sPassword = request.getParameter("Password");
		String sidRoles = request.getParameter("idRoles");
		
		int iTipoPersona = Integer.parseInt(sTipoPersona);
		int iIdRoles = Integer.parseInt(sidRoles);
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");		    
		java.util.Date utilDateIncidente = new Date();
		try {
			utilDateIncidente = format.parse(sFechaNac);
		} catch (ParseException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}	
	    java.sql.Date sqlDateIncidente = new java.sql.Date(utilDateIncidente.getTime());	
		
		GestionPersona oPer = new GestionPersona();
		try {
			oPer.insertar(iTipoPersona, sNombre, sPaterno, sMaterno, sSexo, sTipoDoc, sNroDoc,
					sMail, sCelular, sqlDateIncidente,iIdRoles,sNickname,sPassword);
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		RequestDispatcher rd = request.getRequestDispatcher("Persona.jsp");
		rd.forward(request, response);

		
		
		
	}

}
